package com.mini.controller;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mini.entity.*;
import com.mini.service.UserImgService;
import com.mini.service.VaccineUserInfoService;
import com.mini.utils.FileUpLoadUtil;
//import com.mini.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-04-28
 */
@RestController
@CrossOrigin
@RequestMapping("/mini/userImg")
@Api(description = "疫苗接种", value = "疫苗接种", tags = "疫苗接种")
public class UserImgController {
    @Autowired
    private UserImgService userImgService;

    @GetMapping("/wxLogin/{code}")
    @ApiOperation("微信登录接口")
    public Result wxLogin(@PathVariable(value = "code") String code){
//        String appId = "wx36406f179c9e923a";
//        String appSecret = "aa5ac5d5eb5703274c1b6bafa208ae2c";
//        String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="
//                + appId
//                + "&secret="
//                + appSecret
//                + "&js_code="
//                + code
//                + "&grant_type=authorization_code";
        String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="
                + ConstantValue.APP_ID
                + "&secret="
                + ConstantValue.APP_SECRET
                + "&js_code="
                + code
                + "&grant_type=authorization_code";
        String body = HttpRequest.get(apiUrl).execute().body();
        return Result.success(body);
    }

    @PostMapping("/verify")
    @ApiOperation(value = "图像识别是否为证明,已测无误")
    public Result save(String imgUrl) throws IOException, JSONException {
        boolean ImgRet = userImgService.checkImage(imgUrl);
        return ImgRet ? Result.success("识别成功") : Result.error("识别失败，请上传接种证明");
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Result saveI(@RequestBody UserImgVo userImgVo) {
        UserImg userImg = new UserImg();
        BeanUtils.copyProperties(userImgVo, userImg);
        return userImgService.save(userImg) ? Result.success() : Result.error();
    }

    @PutMapping
    @ApiOperation(value = "改")
    public Result update(@RequestBody UserImg userImg) {
        return userImgService.updateById(userImg) ? Result.success() : Result.error();
    }

    @GetMapping("/find/{current}/{size}/{userId}")
    @ApiOperation(value = "查")
    public Result select(@PathVariable(value = "current", required = true) Integer current,
                         @PathVariable(value = "size", required = true) Integer size,
                         @PathVariable(value = "userId", required = true) String userId) {
        QueryWrapper<UserImg> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Page<UserImg> page = new Page<>(current, size);
        userImgService.page(page, wrapper);
        return Result.success().data(page);
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删")
    public Result delete(@PathVariable(value = "userId", required = true) String id) {
        return userImgService.removeById(id) ? Result.success() : Result.error();
    }
}

